package com.dotflat.application.service.impl;

import com.dotflat.application.domain.AppForm;
import com.dotflat.application.po.LeadsPO;
import com.dotflat.application.po.ParentUserPO;
import com.dotflat.application.repository.AppFormRepository;
import com.dotflat.application.service.AppFormService;
import com.dotflat.rest.PO.AreableObjectBasePO;
import com.dotflat.rest.service.impl.AreableObjectBaseServiceImpl;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.utils.DateUtil;
import com.starinside.wp.utils.MapConvertUtil;
import com.starinside.wp.utils.RemoteCallUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service
public class AppFormServiceImpl extends AreableObjectBaseServiceImpl<AppForm, AppFormRepository> implements AppFormService {

    @Autowired
    @Override
    public void setBaseRepository(AppFormRepository repository){
        super.setBaseRepository(repository);
    }

    @Value("${server.crm.url}")
    private String crmServerURL;

    public LeadsPO addLeadsByAppForm(AppForm appForm, WxUser loginUser){

        LeadsPO param = new LeadsPO();
        param.setBirthday(appForm.getBirthday());
        param.setChannel("入园申请表");  //渠道
//        param.setFromUser(String.format("%s-%s", appForm.getParentName(), appForm.getPhone()));
//        if (loginUser != null) {
//            param.setFromUserId(loginUser.getId()); //介绍人id
//        }

        param.setChildName(appForm.getName());  //孩子姓名
        param.setGender(appForm.getGender()); //性别
        param.setBirthday(appForm.getBirthday());  //孩子生日
        param.setNickName(appForm.getNickName()); //孩子昵称
        param.setEnglishName("");

//        param.setNationality(appForm.getNationality()); //国籍
//        param.setTargetGarden(appForm.getTargetGarden()); //目标幼儿园
//        param.setRegisterResidence(appForm.getRegisterResidence()); //户籍所在地
//        param.setRegisterType(appForm.getRegisterType()); //户籍类型
//        param.setLanguageEnvironment(appForm.getLanguageEnvironment());//所处语言环境

//        ParentUserPO parent = children.getLinkMans().get(0);

        param.setName(appForm.getParentName());  //家长姓名
        param.setRelationship(appForm.getRelation()); //关系
        param.setMobile(appForm.getPhone()); //联系电话
        param.setHomeAddress(appForm.getAddress()); //家庭住址
//        param.setCompany(appForm.getCompany()); //工作单位
//        param.setDuty(parent.getDuty()); //职务
//        param.setDegree(parent.getDegree()); // 学历")

        param.setAreaId(appForm.getAreaId());
//        param.setStaffId(staff.getId());
        param.setStaffName("入园申请表");

        //最近跟进记录
        param.setLastFollowRecord(String.format("家长端用户（%s-%s）入园申请表主动申请",
                appForm.getParentName(), appForm.getPhone(), DateUtil.formatDayToStr(new Date())));

        Map p = MapConvertUtil.obejctToMap(param);
        p.put("source", "家长端用户（%s-%s）入园申请表主动申请");

        //patch for birthday/lastFollowTime
        if (appForm.getBirthday() != null) {
            p.put("birthday", DateUtil.formatDayToStr(appForm.getBirthday()));
        }

        //先添加CRM客户
        LeadsPO result = RemoteCallUtil.call(crmServerURL + "/api/other_system/create_leads", p, null, LeadsPO.class, "创建线索失败");

        return result;
    }

//    @Override
//    public AppForm add(String areaId, String staffId, AreableObjectBasePO param) {
//
//        AppForm result = super.add(areaId, staffId, param);
//
//        LeadsPO po = addLeadsByAppForm(result);
//
//        return result;
//    }
}